//
//  BZYCityController.m
//  BZYMeituan
//
//  Created by 毕仲阳 on 16/9/16.
//  Copyright © 2016年 mycompany. All rights reserved.
//

#import "BZYCityController.h"
#import "BZYCityGroupModel.h"
#import "BZYSearchTableViewController.h"
@interface BZYCityController ()<UITableViewDelegate,UITableViewDataSource,UISearchBarDelegate>
@property (weak, nonatomic) IBOutlet UISearchBar *searchBar;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIButton *coverButton;
@property (nonatomic,strong) NSArray *cityGroupArray;
@property (nonatomic,weak) BZYSearchTableViewController *citySearchVC;
@end

@implementation BZYCityController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self setNav];
    //设置城市分组数据
    NSArray *cityGroupList = [[NSArray alloc]initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"cityGroups.plist" ofType:nil]];
    self.cityGroupArray = [NSArray yy_modelArrayWithClass:[BZYCityGroupModel class] json:cityGroupList];
}
- (void)setNav{
    self.title = @"切换城市";
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem barBuutonItemWithTarget:self action:@selector(backItemClick) icon:@"btn_navigation_close" highlighticon:@"btn_navigation_close_hl"];
}

- (void)backItemClick
{
    [self dismissViewControllerAnimated:true completion:nil];
}
#pragma mark -UISearchBarDelegate
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
    //NSLog(@"nnnn");
    self.navigationController.navigationBarHidden = true;
    
    searchBar.backgroundImage = [UIImage imageNamed:@"bg_login_textfield_hl"];
    [searchBar setShowsCancelButton:true];
    self.coverButton.alpha = 0.5;
}

- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar{
    self.navigationController.navigationBarHidden = false;
    
    searchBar.backgroundImage = [UIImage imageNamed:@"bg_login_textfield"];
    [searchBar setShowsCancelButton:false animated:true];
    
    self.coverButton.alpha = 0;
//    searchBar.text = @"";
//    self.citySearchVC.view.hidden = true;
    
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{
    [searchBar resignFirstResponder];
}

/**
 遮盖按钮点击事件

 @param sender <#sender description#>
 */
- (IBAction)coverButtonClick:(id)sender {
    
    [self.view endEditing:true];
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    if (searchText.length > 0) {
        self.citySearchVC.view.hidden = false;
        self.citySearchVC.searchText = searchText;
    }else
    {
        self.citySearchVC.view.hidden = true;
    }
}


#pragma mark -UITableViewDelegate,UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.cityGroupArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    BZYCityGroupModel *model = self.cityGroupArray[section];
    return model.cities.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ID = @"cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
    }
    
    //1. 先获取模型数据
    BZYCityGroupModel *cityGroupModel = self.cityGroupArray[indexPath.section];
    cell.textLabel.text = cityGroupModel.cities[indexPath.row];
    
    return cell;

}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    BZYCityGroupModel *model = self.cityGroupArray[section];
    return model.title;
}


- (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    
    return [self.cityGroupArray valueForKey:@"title"];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    BZYCityGroupModel *model = self.cityGroupArray[indexPath.section];
    //发送通知
    [[NSNotificationCenter defaultCenter] postNotificationName:BZYCityDidChangeNotification object:nil userInfo:@{BZYCityNameKey : model.cities[indexPath.row]}];
    
    [self dismissViewControllerAnimated:true completion:nil];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


/**
 懒加载控件

 */
- (BZYSearchTableViewController *)citySearchVC
{
    if (_citySearchVC == nil) {
        BZYSearchTableViewController *citySearchVC = [[BZYSearchTableViewController alloc]init];
        
        [self addChildViewController:citySearchVC];
        [self.view addSubview:citySearchVC.view];
        [citySearchVC.view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.coverButton);
        }];
        _citySearchVC = citySearchVC;
    }
    return _citySearchVC;
   
}
@end
